﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Data;
using MyWebPagesStarterKit;

namespace MyWebPagesStarterKit
{
    public class MailTemplate : Persistable<MailTemplate.Data>
    {
        private static MailTemplate _instance;

        public MailTemplate()
        {
            base._data.Entries = new DataSet();
            base._data.Entries.Tables.Add(new DataTable("Entries"));
            DataColumn column = new DataColumn("Guid", typeof(Guid));
            column.Unique = true;
            base._data.Entries.Tables["Entries"].Columns.Add(column);
            base._data.Entries.Tables["Entries"].PrimaryKey = new DataColumn[] { column };
            base._data.Entries.Tables["Entries"].Columns.AddRange(new DataColumn[] { 
                new DataColumn("Title", typeof(string)), //Titel i denna lista
                new DataColumn("TitleUnique", typeof(string)), //Titel för anrop, unik
                new DataColumn("Subject", typeof(string)), //Ämne på epostmeddelandet
                new DataColumn("Description", typeof(string)) //Möjliga fält att använda i epostmallen
            });
        }

        public DataView GetEntries(string title)
        {
            return new DataView(Entries, string.Format("TitleUnique='{0}'", title), "", DataViewRowState.CurrentRows);
        }


        public DataRow GetRowFromTitle(string title)
        {
            DataRow row = null;
            DataRow[] foundRows = Entries.Select(string.Format("TitleUnique = '{0}'", title));
            if (foundRows.Length > 0)
                row = foundRows[0];
            return row;
        }

        public DataView GetEntriesFromKey(string key)
        {
            return new DataView(Entries, string.Format("Guid='{0}'", key), "", DataViewRowState.CurrentRows);
        }

        public DataRow GetRowFromKey(string key)
        {
            DataRow row = null;
            DataRow[] foundRows = Entries.Select(string.Format("Guid = '{0}'", key));
            if (foundRows.Length > 0)
                row = foundRows[0];
            return row;
        }

        public DataView GetEntries()
        {
            return new DataView(Entries, null, "Title DESC", DataViewRowState.CurrentRows);
        }

        public DataTable Entries
        {
            get
            {
                return base._data.Entries.Tables["Entries"];
            }
        }

        public static MailTemplate GetInstance()
        {
            if (_instance == null)
            {
                _instance = new MailTemplate();
                if (File.Exists(HttpContext.Current.Server.MapPath(_instance.GetDataFilename())))
                {
                    _instance.LoadData();
                }
            }
            return _instance;
        }

        protected override string GetDataFilename()
        {
            return "~/App_Data/MailTemplate/MailTemplate.config";
        }

        public class Data
        {
            public DataSet Entries;
        }

    }
}